/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.cs.store.BlobInputStream;
/*     */ import java.io.File;
/*     */ import java.io.FileWriter;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.Random;
/*     */ import javax.mail.internet.SharedInputStream;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestBlobInputStream
/*     */   extends TestCase
/*     */ {
/*     */   private File mFile;
/*     */   private String mOrigBufferSize;
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  43 */     this.mOrigBufferSize = TestUtil.getServerAttr("zimbraMailFileDescriptorBufferSize");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testBlobInputStream()
/*     */     throws Exception
/*     */   {
/*  51 */     int[] bufferSizes = { 0, 1, 4, 5, 9, 10, 99, 999, 1000, 2000 };
/*  52 */     for (int bufferSize : bufferSizes) {
/*  53 */       TestUtil.setServerAttr("zimbraMailFileDescriptorBufferSize", Integer.toString(bufferSize));
/*  54 */       runBlobInputStreamTest();
/*  55 */       runLargeFileTest();
/*     */     }
/*     */   }
/*     */   
/*     */   public void runBlobInputStreamTest() throws Exception
/*     */   {
/*  61 */     String CONTENT = "0123456789";
/*  62 */     createFile(CONTENT);
/*  63 */     BlobInputStream in = new BlobInputStream(this.mFile, this.mFile.length());
/*     */     
/*     */ 
/*  66 */     String read = getContent(in, 100);
/*  67 */     assertEquals(CONTENT, read);
/*  68 */     checkEof(in);
/*  69 */     in.close();
/*     */     
/*     */ 
/*  72 */     in = new BlobInputStream(this.mFile, this.mFile.length());
/*  73 */     assertEquals("01234", getContent(in, 5));
/*  74 */     assertEquals("56789", getContent(in, 100));
/*  75 */     checkEof(in);
/*  76 */     in.close();
/*     */     
/*     */     try
/*     */     {
/*  80 */       in = new BlobInputStream(this.mFile, this.mFile.length(), Long.valueOf(6L), Long.valueOf(5L));
/*  81 */       fail("Test with start=6 and end=5 should not have succeeded.");
/*     */     }
/*     */     catch (IOException e) {}
/*     */     
/*     */ 
/*  86 */     in = new BlobInputStream(this.mFile, this.mFile.length());
/*  87 */     assertEquals(2L, in.skip(2L));
/*  88 */     assertEquals("23", getContent(in, 2));
/*  89 */     assertEquals(3L, in.skip(3L));
/*  90 */     assertEquals("7", getContent(in, 1));
/*  91 */     assertEquals(2L, in.skip(1000L));
/*  92 */     checkEof(in);
/*  93 */     in.close();
/*     */     
/*     */ 
/*  96 */     in = new BlobInputStream(this.mFile, this.mFile.length());
/*  97 */     assertTrue(in.markSupported());
/*  98 */     boolean success = true;
/*     */     try {
/* 100 */       in.reset();
/*     */     } catch (IOException e) {
/* 102 */       success = false;
/*     */     }
/* 104 */     assertFalse("reset() should not have succeeded", success);
/* 105 */     assertEquals("012", getContent(in, 3));
/* 106 */     in.mark(3);
/* 107 */     assertEquals("34", getContent(in, 2));
/* 108 */     in.reset();
/* 109 */     assertEquals("34", getContent(in, 2));
/* 110 */     assertEquals("56", getContent(in, 2));
/* 111 */     success = true;
/*     */     try {
/* 113 */       in.reset();
/*     */     } catch (IOException e) {
/* 115 */       success = false;
/*     */     }
/* 117 */     assertFalse("reset() should not have succeeded", success);
/* 118 */     in.close();
/*     */     
/*     */ 
/* 121 */     in = new BlobInputStream(this.mFile, this.mFile.length());
/* 122 */     byte[] buf = new byte[5];
/* 123 */     for (int i = 0; i < 5; i++) {
/* 124 */       buf[i] = 57;
/*     */     }
/* 126 */     int numRead = in.read(buf, 3, 2);
/* 127 */     assertTrue("Unexpected number of bytes read: " + numRead, (numRead == 1) || (numRead == 2));
/* 128 */     int[] untouchedIndexes = null;
/* 129 */     if (numRead == 1) {
/* 130 */       assertEquals((byte)48, buf[3]);
/* 131 */       untouchedIndexes = new int[] { 0, 1, 2, 4 };
/*     */     }
/* 133 */     if (numRead == 2) {
/* 134 */       assertEquals((byte)48, buf[3]);
/* 135 */       assertEquals((byte)49, buf[4]);
/* 136 */       untouchedIndexes = new int[] { 0, 1, 2 };
/*     */     }
/* 138 */     for (int i : untouchedIndexes) {
/* 139 */       assertEquals("Unexpected value at index " + i, 57, buf[i]);
/*     */     }
/* 141 */     in.close();
/*     */     
/*     */ 
/* 144 */     in = new BlobInputStream(this.mFile, this.mFile.length());
/* 145 */     in.read();
/* 146 */     in.read();
/* 147 */     numRead = in.read(buf);
/* 148 */     assertTrue(numRead > 0);
/* 149 */     assertTrue(numRead <= 5);
/* 150 */     byte[] test = new byte[numRead];
/* 151 */     System.arraycopy(buf, 0, test, 0, numRead);
/* 152 */     assertTrue("23456".startsWith(new String(test)));
/* 153 */     in.close();
/*     */     
/*     */ 
/* 156 */     InputStream sub = in.newStream(0L, CONTENT.length());
/* 157 */     assertEquals(CONTENT, getContent(sub, 100));
/* 158 */     checkEof(sub);
/* 159 */     sub.close();
/*     */     
/*     */ 
/* 162 */     sub = in.newStream(0L, 5L);
/* 163 */     assertEquals("01234", getContent(sub, 100));
/* 164 */     checkEof(sub);
/* 165 */     sub.close();
/*     */     
/*     */ 
/* 168 */     sub = in.newStream(5L, 10L);
/* 169 */     assertEquals("56789", getContent(sub, 100));
/* 170 */     checkEof(sub);
/* 171 */     sub.close();
/*     */     
/* 173 */     sub = in.newStream(5L, -1L);
/* 174 */     assertEquals("56789", getContent(sub, 100));
/* 175 */     checkEof(sub);
/* 176 */     sub.close();
/*     */     
/*     */ 
/* 179 */     assertEquals(null, in.newStream(5L, 11L));
/*     */     
/*     */ 
/* 182 */     sub = in.newStream(3L, 6L);
/* 183 */     assertEquals("345", getContent(sub, 100));
/* 184 */     checkEof(sub);
/* 185 */     sub.close();
/*     */     
/*     */ 
/* 188 */     sub = in.newStream(3L, 6L);
/* 189 */     assertEquals(0L, ((SharedInputStream)sub).getPosition());
/* 190 */     sub.read(new byte[2]);
/* 191 */     assertEquals(2L, ((SharedInputStream)sub).getPosition());
/* 192 */     sub.close();
/*     */     
/*     */ 
/* 195 */     InputStream subsub = ((BlobInputStream)sub).newStream(1L, 3L);
/* 196 */     assertEquals("45", getContent(subsub, 100));
/*     */     
/*     */ 
/* 199 */     in.close();
/* 200 */     in = new BlobInputStream(this.mFile, this.mFile.length());
/* 201 */     assertEquals(0L, in.getPosition());
/* 202 */     in.read();
/* 203 */     assertEquals(1L, in.getPosition());
/* 204 */     in.close();
/*     */     
/*     */ 
/* 207 */     in = new BlobInputStream(this.mFile, this.mFile.length());
/* 208 */     buf = new byte[4];
/* 209 */     while ((numRead = in.read(buf)) >= 0) {}
/*     */     
/* 211 */     in.close();
/*     */     
/* 213 */     this.mFile.delete();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void runLargeFileTest()
/*     */     throws Exception
/*     */   {
/* 221 */     String content = createFile(5000);
/* 222 */     BlobInputStream in = new BlobInputStream(this.mFile, this.mFile.length());
/* 223 */     assertEquals(content, getContent(in, 5000));
/* 224 */     in.close();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 229 */     in = new BlobInputStream(this.mFile, this.mFile.length());
/* 230 */     String firstChunk = getContent(in, 1000);
/* 231 */     assertEquals(content.substring(0, 1000), firstChunk);
/*     */     
/* 233 */     byte[] secondChunk = new byte['ߐ'];
/* 234 */     int numRead = in.read(secondChunk);
/* 235 */     assertTrue(numRead > 0);
/* 236 */     byte[] test = new byte[numRead];
/* 237 */     System.arraycopy(secondChunk, 0, test, 0, numRead);
/* 238 */     assertEquals(content.substring(1000, 1000 + numRead), new String(test));
/* 239 */     int thirdChunkStartPos = 1000 + numRead;
/*     */     
/*     */ 
/*     */ 
/* 243 */     byte[] thirdChunk = new byte['ߐ'];
/* 244 */     numRead = in.read(thirdChunk);
/* 245 */     assertTrue(numRead > 0);
/* 246 */     test = new byte[numRead];
/* 247 */     System.arraycopy(thirdChunk, 0, test, 0, numRead);
/* 248 */     assertEquals(content.substring(thirdChunkStartPos, thirdChunkStartPos + numRead), new String(thirdChunk));
/*     */     
/* 250 */     this.mFile.delete();
/* 251 */     in.close();
/*     */   }
/*     */   
/*     */   private String createFile(int numBytes) throws Exception
/*     */   {
/* 256 */     StringBuilder buf = new StringBuilder();
/* 257 */     Random random = new Random();
/* 258 */     for (int i = 0; i < numBytes; i++) {
/* 259 */       char c = 'a';
/* 260 */       c = (char)(c + random.nextInt(26));
/* 261 */       buf.append(c);
/*     */     }
/* 263 */     String s = buf.toString();
/* 264 */     createFile(s);
/* 265 */     return s;
/*     */   }
/*     */   
/*     */   private void createFile(String data) throws Exception
/*     */   {
/* 270 */     this.mFile = File.createTempFile("TestBlobInputStream", ".txt");
/* 271 */     FileWriter writer = new FileWriter(this.mFile);
/* 272 */     writer.write(data);
/* 273 */     writer.close();
/*     */   }
/*     */   
/*     */   private String getContent(InputStream in, int maxBytes) throws Exception
/*     */   {
/* 278 */     StringBuilder builder = new StringBuilder();
/* 279 */     for (int i = 1; i <= maxBytes; i++) {
/* 280 */       int c = in.read();
/* 281 */       if (c <= 0) {
/*     */         break;
/*     */       }
/* 284 */       builder.append((char)c);
/*     */     }
/* 286 */     return builder.toString();
/*     */   }
/*     */   
/*     */   private void checkEof(InputStream in) throws Exception
/*     */   {
/* 291 */     assertEquals(-1, in.read());
/* 292 */     byte[] buf = new byte[10];
/* 293 */     assertEquals(-1, in.read(buf));
/* 294 */     assertEquals(-1, in.read(buf, 5, 2));
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception
/*     */   {
/* 299 */     if ((this.mFile != null) && (this.mFile.exists())) {
/* 300 */       this.mFile.delete();
/*     */     }
/* 302 */     TestUtil.setServerAttr("zimbraMailFileDescriptorBufferSize", this.mOrigBufferSize);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestBlobInputStream.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */